<?php
namespace app\controller\manager;

use app\BaseController;
use app\model\VTeacherCourse;
use app\model\Course;
use app\model\Teacher;
use DateTime;
use think\Request;
use \think\facade\Db;

/**
 * 课程控制器
 * @todo 完善增删改查
 * @todo 完善excel导入
 */
class CourseManage extends BaseController
{
    public function list()
    {
        $pageSize = $this->request->param('pageSize',10);
        $courseName = $this->request->param('courseName','');
        $TID = $this->request->param('TID','not null');
        $course = VTeacherCourse::where('TID',$TID)->whereLike('courseName','%'.$courseName.'%')->paginate($pageSize);
        //echo(VTeacherCourse::getLastSql());
        return resultSuccess($course);
    }

     /**
     * 增加课程
     */
    public function addCourse(Request $request)
    {
        if ($request->isPost()) {
            if(!Teacher::find($request->param('TID'))){
                return resultError('无此教师');
            }
            $course = new Course;
            if ($course->save(
                [
                    'courseName' => $request->param('courseName'),
                    'TID' => $request->param('TID'),
                    'createTime' => date("Y-m-d H:i:s")
                ]
            )) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 删除课程
     */
    public function deleteCourse(Request $request)
    {
        if ($request->isGet()) {
            $course = Course::find($request->param('courseID'));
            if ($course->delete()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 修改课程信息
     */
    public function editCourse(Request $request)
    {
        if ($request->isPost()) {
            $course = Course::find($request->param('courseID'));
            $course->courseName = $request->param('courseName');
            if ($course->save()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }
}
